package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.witsky.WitskyMobileDn;
import com.ucredit.babel.common.model.third.witsky.WitskyMobileDnData;
import com.ucredit.babel.common.model.third.witsky.WitskyMobileDnRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 诚智天扬号码状态监测
 *
 * @author renzhanzheng
 */

public class WitskyMobileDnParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 诚智天扬号码状态监测
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        WitskyMobileDnRecord obj = (WitskyMobileDnRecord)model;
        List<WitskyMobileDnData> data = obj.getData() == null ? new ArrayList<>() : obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder detailSb = new StringBuilder(500);
        int i = 0;
        for (WitskyMobileDnData d : data) {
            i++;
            detailSb.delete(0, detailSb.length());
            //序号
            detailSb.append(i).append(CRT001);
            //返回码
            detailSb.append(d.getCode() == null ? "" : d.getCode()).append(CRT001);
            //返回码描述
            detailSb.append(d.getMessage() == null ? "" : d.getMessage()).append(CRT001);
            WitskyMobileDn dataScore = d.getData() == null ? new WitskyMobileDn() : d.getData();
            //交易ID
            detailSb.append(dataScore.getRequestId() == null ? "" : dataScore.getRequestId()).append(CRT001);
            //查询结果
            detailSb.append(dataScore.getResult() == null ? "" : dataScore.getResult()).append(CRT001);
            //手机状态
            detailSb.append(dataScore.getState() == null ? "" : dataScore.getState()).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString()).replace("\n", "").replace("\r", " "));
            context.write(key, val);
        }

        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_czty_onlinestatus_inc";
    }

    @Override
    public String getType() {
        return RecordType.WITSKY_MOBILEDN_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "诚智天扬号码状态监测";
    }
}
